import { save, pages, LoadFrom, Delete, CloseCompany, CreateMiniCode } from '@/api/companyInfo/companyInfo'

import { pages as IndustryPage } from '@/api/companyInfo/Industry'

import { pages as WelfarePage } from '@/api/companyInfo/welfare'
// import { DepartmentTree } from '@/api/systems/systemDepartment'

const state = {
  controllerName: 'companyInfo',
  dataList: {
    listLoading: false,
    list: [],
    departmentTree: [],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      LoginName: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: { Welfare: [] },
    ResetPwdFormState: false,
    ResetPwdModel: {},
    AddressOptions: [],
    IndustrysList: [],
    WelfareList: [],
    resState: false
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`companyInfo/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`companyInfo/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    LoadFrom({ ID: par })
      .then(response => {
        const { Data } = response
        console.log(Data)
        Data.Welfare = []
        _state.form.model = Data
        _state.form.formState = true

        if (_state.form.model.CompanyWelfare) {
          _state.form.model.Welfare = _state.form.model.CompanyWelfare.split(',')
        }
        console.log(_state.form.model.Welfare)

        context.dispatch('getIndustryList')
        context.dispatch('getWelfareList', _state.form.model.ID)
      })
      .catch(error => {
        console.log(error)
      })
  },
  save(context, par) {
    var _state = context.state
    console.log(par, '保存')
    // if (par.CompanyLicense === '' || par.CompanyLicense === null) {
    //   global.tools.msg('请上传营业执照!', '错误')
    //   return
    // }
    if (par.ContactName === '' || par.ContactName === null) {
      global.tools.msg('请输入联系人!', '错误')
      return
    }
    if (par.ContactPhone === '' || par.ContactPhone === null) {
      global.tools.msg('请输入联系人手机号!', '错误')
      return
    }
    if (par.CompanyName === '' || par.CompanyName === null) {
      global.tools.msg('请输入公司简称!', '错误')
      return
    }
    // if (par.CompanyName.length > 8) {
    //   global.tools.msg('公司简称限制1-8位!', '错误')
    //   return
    // }
    _state.form.model.CompanyWelfare = _state.form.model.Welfare.join(',')
    save(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('保存成功!', '成功')
          _state.form.formState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('保存失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  getIndustryList(context) {
    var _state = context.state
    IndustryPage({
      pageIndex: 1,
      pageCount: 1000
    })
      .then(res => {
        const { Data } = res
        _state.form.IndustrysList = Data.List
        console.log('所属行业', _state.form.IndustrysList)
      })
      .catch(e => {
        console.log(e)
      })
  },
  getWelfareList(context, par) {
    var _state = context.state
    WelfarePage({
      CompanyId: par,
      pageIndex: 1,
      pageCount: 1000
    })
      .then(res => {
        const { Data } = res
        _state.form.WelfareList = Data.List
        console.log(_state.form.WelfareList)
      })
      .catch(e => {
        console.log(e)
      })
  },
  closeCompany(context, par) {
    var _state = context.state
    console.log(par)
    var alert_msg = '确定要关闭该公司吗？关闭后该公司所有账号不能使用。'
    if (par.type === 0) {
      alert_msg = '确定要开启该公司吗？'
    }
    global.tools.confirm(
      alert_msg,
      function() {
        CloseCompany(par)
          .then(res => {
            const { Data } = res
            console.log(Data)
            _state.form.resState = Data
            if (_state.form.resState) {
              global.tools.msg('操作成功!', '成功')
              _state.form.formState = false
              context.dispatch('findList')
            } else {
              global.tools.msg('操作失败!', '错误')
            }
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  createMiniCode(context, par) {
    var _state = context.state
    console.log(par)
    CreateMiniCode(par)
      .then(res => {
        const { Data } = res
        console.log(Data, 'minicode')
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('生成成功!', '成功')
          _state.form.formState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('生成失败!', '错误')
        }
      })
      .catch(e => {
        console.log(e)
      })
      
  },
  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.CompanyLogo = res.Data.Url
      console.log('_state.form.model.CompanyLogo', _state.form.model.CompanyLogo)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  },
  handleSuccessLicence(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.CompanyLicense = res.Data.Url
      console.log('_state.form.model.CompanyLicense', _state.form.model.CompanyLicense)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
